/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Acceleration;
	ref class GeometryInstance;

	/// <summary>
	/// A container for an arbitrary number of <see cref="OptixDotNet::GeometryInstance">GeometryInstances</see>.
    /// </summary>
	public ref class GeometryGroup : ContainerNode, IContainerNode<GeometryInstance^>, INodeCollectionProvider<GeometryInstance^>
	{
	public:

		/// <summary>
		/// Creates a new GeometryGroup node.
		/// </summary>
		GeometryGroup( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			CheckError( rtGeometryGroupValidate( mGeoGroup ) );
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			if( mGeoGroup )
				CheckError( rtGeometryGroupDestroy( mGeoGroup ) );

			mGeoGroup = 0;
		}

		/// <summary>
		/// Gets the NodeCollection containded by the GeometryGroup
		/// NodeCollection is comprised of nodes of: GeometryInstance
        /// </summary>
		property NodeCollection<GeometryInstance^>^ Children
		{
			virtual NodeCollection<GeometryInstance^>^ get() = INodeCollectionProvider<GeometryInstance^>::Collection::get
			{
				return mCollection;
			}
		}

		/// <summary>
		/// Gets or Sets the number of child nodes to be attached to the geometry group.
        /// </summary>
		property unsigned int ChildCount
		{
			virtual unsigned int get() = IContainerNode<GeometryInstance^>::ChildCount::get
			{
				unsigned int count;
				CheckError( rtGeometryGroupGetChildCount( mGeoGroup, &count ) );
				
				return count;
			}
			virtual void set( unsigned int count ) = IContainerNode<GeometryInstance^>::ChildCount::set
			{
				CheckError( rtGeometryGroupSetChildCount( mGeoGroup, count ) );
			}
		}

		/// <summary>
		/// Gets the child GeometryInstance attached to a Selector node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the GeometryInstance</param>
		/// <returns>Returns the attached Geometry Instance.</returns>
		GeometryInstance^ GetChild( unsigned int index );

		/// <summary>
		/// Attaches a GeometryInstance to a GeometryGroup at [index]
        /// </summary>
		/// <param name="index">Index of the geometry instance</param>
		/// <param name="instance">Geometry Instance to attach</param>
		void SetChild( unsigned int index, GeometryInstance^ instance );

		/// <summary>
		/// Attaches a GeometryInstance to the end of a GeometryGroup node child list
        /// </summary>
		/// <param name="instance">instance to add</param>
		void AddChild( GeometryInstance^ instance )
		{
			SetChild( ChildCount++, instance );
		}

		/// <summary>
		/// Attaches an enumberable list of GeometryInstances to the end of a GeometryGroup node child list
        /// </summary>
		/// <param name="instances">list of instances to add</param>
		void AddChildren( System::Collections::Generic::IEnumerable<GeometryInstance^>^ instances )
		{
			if( instances == nullptr )
				return;

			for each( GeometryInstance^ gi in instances)
			{
				SetChild( ChildCount++, gi );
			}
		}

		/// <summary>
		/// Gets or Sets the acceleratoin structure for a geometry group
        /// </summary>
		property OptixDotNet::Acceleration^ Acceleration
		{
			OptixDotNet::Acceleration^ get();
			void set( OptixDotNet::Acceleration^ accel );
		}

	internal:
		GeometryGroup( Context^ context, RTgeometrygroup group );
		inline RTgeometrygroup InternalPtr() { return mGeoGroup; }
		virtual RTobject ObjectPtr() override { return InternalPtr(); }

		/// <summary>
		/// Gets or Attaches a child node to a selector at [index]
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property GeometryInstance^ ChildNodes[unsigned int]
		{
			virtual GeometryInstance^ get ( unsigned int index ) = IContainerNode<GeometryInstance^>::ChildNodes::get;
			virtual void set ( unsigned int index, GeometryInstance^ node ) = IContainerNode<GeometryInstance^>::ChildNodes::set;
		}

	private:

		RTgeometrygroup	mGeoGroup;
		NodeCollection<GeometryInstance^>^ mCollection;
	};
}